package com.example.chinawenddingexpo.controller;

import com.example.chinawenddingexpo.model.WeddingService;
import com.example.chinawenddingexpo.service.WeddingServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/wedding-service")
public class WeddingServiceController {
    
    @Autowired
    private WeddingServiceService weddingServiceService;
    
    // 获取用户的服务预订
    @GetMapping("/user/{userId}")
    public Map<String, Object> getUserServices(@PathVariable Long userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<WeddingService> services = weddingServiceService.getUserServices(userId);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", services);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取用户服务失败");
        }
        return result;
    }
    
    // 按状态获取用户服务
    @GetMapping("/user/{userId}/status/{status}")
    public Map<String, Object> getUserServicesByStatus(@PathVariable Long userId, 
                                                      @PathVariable WeddingService.BookingStatus status) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<WeddingService> services = weddingServiceService.getUserServicesByStatus(userId, status);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", services);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取用户服务失败");
        }
        return result;
    }
    
    // 获取即将进行的服务
    @GetMapping("/user/{userId}/upcoming")
    public Map<String, Object> getUpcomingServices(@PathVariable Long userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<WeddingService> services = weddingServiceService.getUpcomingServices(userId);
            result.put("code", 200);
            result.put("msg", "success");
            result.put("data", services);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取即将进行服务失败");
        }
        return result;
    }
    
    // 预订服务
    @PostMapping("/book")
    public Map<String, Object> bookService(@RequestBody WeddingService weddingService) {
        Map<String, Object> result = new HashMap<>();
        try {
            WeddingService savedService = weddingServiceService.saveService(weddingService);
            result.put("code", 200);
            result.put("msg", "预订成功");
            result.put("data", savedService);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "预订服务失败");
        }
        return result;
    }
    
    // 确认预订
    @PutMapping("/{serviceId}/confirm")
    public Map<String, Object> confirmBooking(@PathVariable Long serviceId) {
        Map<String, Object> result = new HashMap<>();
        try {
            WeddingService service = weddingServiceService.confirmBooking(serviceId);
            if (service != null) {
                result.put("code", 200);
                result.put("msg", "确认成功");
                result.put("data", service);
            } else {
                result.put("code", 404);
                result.put("msg", "服务不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "确认预订失败");
        }
        return result;
    }
    
    // 取消预订
    @PutMapping("/{serviceId}/cancel")
    public Map<String, Object> cancelBooking(@PathVariable Long serviceId) {
        Map<String, Object> result = new HashMap<>();
        try {
            WeddingService service = weddingServiceService.cancelBooking(serviceId);
            if (service != null) {
                result.put("code", 200);
                result.put("msg", "取消成功");
                result.put("data", service);
            } else {
                result.put("code", 404);
                result.put("msg", "服务不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "取消预订失败");
        }
        return result;
    }
    
    // 获取服务详情
    @GetMapping("/{serviceId}")
    public Map<String, Object> getServiceDetail(@PathVariable Long serviceId) {
        Map<String, Object> result = new HashMap<>();
        try {
            WeddingService service = weddingServiceService.getServiceById(serviceId);
            if (service != null) {
                result.put("code", 200);
                result.put("msg", "success");
                result.put("data", service);
            } else {
                result.put("code", 404);
                result.put("msg", "服务不存在");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "获取服务详情失败");
        }
        return result;
    }
}